//
//  MFWDataModel.m
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-7-24.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "MFWDataModel.h"
#import "MFWHttpRequest.h"
#import "MFWJson.h"

NSString *const mfwDefaultDataId = @"0";

@implementation MFWDataIdentifier
- (id)initWithDataCategory:(NSString*)category dataId:(NSString*)dataId
{
    self = [super init];
    if (self)
    {
        _dataCategoryName = [category copy];
        _dataId = [dataId copy];
    }
    return self;
}

- (BOOL)isEqual:(id)object
{
    if ([object isKindOfClass:[MFWDataIdentifier class]])
    {
        MFWDataIdentifier *anIdentifier = (MFWDataIdentifier*)object;
        if ([anIdentifier.dataCategoryName isEqualToString:self.dataCategoryName]
            && [anIdentifier.dataId isEqualToString:self.dataId])
        {
            return YES;
        }
    }
    return NO;
}

+ (MFWDataIdentifier*)identifierOfCategory:(NSString*)dataCategoryName;
{
    return [self identifierOfCategory:dataCategoryName dataId:mfwDefaultDataId];
}

+ (MFWDataIdentifier*)identifierOfCategory:(NSString*)dataCategory dataId:(NSString*)dataId;
{
    MFWDataIdentifier *identifer = [[MFWDataIdentifier alloc] initWithDataCategory:dataCategory dataId:dataId];
    return identifer;
}

- (NSString*)description
{
    NSMutableString *desc = [NSMutableString stringWithFormat:@"%@ [dataCategoryName: %@, dataId: %@]", NSStringFromClass([self class]), _dataCategoryName, _dataId];
    return desc;
}
@end


@implementation MFWDataModel
+ (NSString*)categoryName;
{
    return NSStringFromClass([self class]);
}

+ (instancetype)managedModelInstance;
{
    return [[MFWDataCenter sharedInstance] dataModelForCategory:[self categoryName]];
}

- (id)init
{
    self = [super init];
    if (self)
    {
        _dataDic = [NSMutableDictionary dictionaryWithCapacity:7];
    }
    
    return self;
}

- (MFWJson*)jsonDataForDataId:(NSString*)dataId;
{
    id data = [_dataDic objectForKey:dataId];
    
    if (data == nil)
    {
        data = [NSMutableDictionary dictionaryWithCapacity:10];
        [_dataDic setValue:data forKey:dataId];
    }
    return [MFWJson jsonWithData:data];
}

- (void)cleanDataForDataId:(NSString*)dataId;
{
    if (dataId)
    {
        [_dataDic removeObjectForKey:dataId];
    }
}

- (MFWHttpRequest*)mfwHttpRequestForTask:(MFWHttpTask*)task;
{
    MFWHttpRequest* request = [MFWHttpRequest dataRequest];
    request.task = task;
    request.httpMethod = kRequestHttpMethodPost;
    
    return request;
}

#pragma mark - MFWHttpTaskCallback methods
- (BOOL)processSuccRawData:(NSData*)rawData parsedJson:(MFWJson*)jsonObj forTask:(MFWHttpTask*)task;
{
    [_dataDic setValue:jsonObj forKey:task.dataIdentifier.dataId];
    return YES;
}

- (void)processFailRawData:(NSData*)rawData parsedJson:(MFWJson*)jsonObj forTask:(MFWHttpTask*)task;
{
}

+ (void)addDataObserver:(id<MFWDataObserver>)anObserver
{
    [[MFWHttpEngine sharedInstance] addDataObserver:anObserver forDataCategory:NSStringFromClass([self class])];
}

+ (void)removeDataObserver:(id<MFWDataObserver>)anObserver;
{
    [[MFWHttpEngine sharedInstance] removeDataObserver:anObserver forDataCategory:[[self class] categoryName]];
}
@end
